/**
*发电机组 js
*/ 

var	dynamoDS;
var dynamoFormPanel;
function doDynamoDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JDynamoWeb.deleteDynamo(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					dynamoDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//dynamoFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var dynamoType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'manufacturer', type: "string"},
							   {name: 'type', type: "string"}, 
							   {name: 'model', type: "string"}, 
							   {name: 'power', type: "string"}, 
							   {name: 'autoboot', type: "string"},
							   {name: 'consumption', type: "string"},
							   {name: 'sumtime', type: "string"},
							   {name: 'maintenance', type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'dynamoIsStatic',  type: 'string'},
							   {name: 'danymoSeiral',  type: 'string'},
							   {name: 'dynamoWorkLen',  type: 'string'},
							   {name: 'dynamoHouse',  type: 'string'},
							   {name: 'dynamoMaintainRec',  type: 'string'}
						 ]);
	var dynamoCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "规格型号",  sortable: true, dataIndex: 'type',align:'left'},	
				{header: "厂家",  sortable: true, dataIndex: 'manufacturer',align:'left'},
				{header: "机型",  sortable: true, dataIndex: 'model',align:'left'}, 
				{header: "功率",  sortable: true, dataIndex: 'power',align:'left'} 				
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doDynamoDelete(\"" + id  +   "\")'>";  
	}
	
	function formatFence(value) 
	{
		var strState = "";
		if(value == "0")
			strState = '<span style="color:red;">' + "否" + '</span>';
		else  
			strState = '是'; 		
		return strState;
	}
	
	dynamoDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JDynamoWeb.getDynamoList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, dynamoType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var dynamoGrid = new Ext.grid.GridPanel({			 	
			ds: dynamoDS,
			cm: dynamoCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            dynamoFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: dynamoDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});

	dynamoDS.on('load', function() {
		dynamoGrid.getSelectionModel().selectFirstRow(); 
	});		
		
 			
	var dynamoIsStaticData = [['0', '否'],['1', '是']];
	
	var dynamoIsStaticStore = new Ext.data.SimpleStore({
		    fields: ['value', 'text'],
		    data : dynamoIsStaticData
		});	
	
	var dynamoIsStaticCombo = new Ext.form.ComboBox({	   
		fieldLabel: '是否有固定油机',
		name: 'dynamoIsStatic',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'dynamoIsStatic',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: dynamoIsStaticStore,
		value:0
	});		
			
	
	var modelStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	
	var modelCombo = new Ext.form.ComboBox({	   
		fieldLabel: '机型',
		name: 'model',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'model',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: modelStore
	});
	modelStore.on('beforeload', function() {
			  modelStore.baseParams = {
					DIRECTORY_TYPE:'09'   
			  };
		});	
	modelStore.load({params:{DIRECTORY_TYPE:'09'}});
	
	var autobootCombo = new Ext.form.ComboBox({	   
		fieldLabel: '是否自启动',
		name: 'autoboot',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'autoboot',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: dynamoIsStaticStore
	});
		
	
	
	var houseStore  = new Ext.data.Store({  
        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
        reader:new Ext.data.DWRArrayReader(
				{},
				new Ext.data.Record.create([  
					{name: 'text', mapping:1},       
					{name: 'value',mapping:0}                   
				  ])
				)  
    }); 

	var houseCombo = new Ext.form.ComboBox({	   
		fieldLabel: '发电机房',
		name: 'dynamoHouse',	
		width:150,
		valueField :"value",
		displayField: "text",
		mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'dynamoHouse',
		triggerAction: 'all', 
		editable:false, 
		disabled:true,
		store: houseStore
		});
	houseStore.on('beforeload', function() {
		houseStore.baseParams = {
				DIRECTORY_TYPE:'34'   
		  };
	});	
	houseStore.load({params:{DIRECTORY_TYPE:'09'}});
	
	dynamoFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 210,
		minSize: 210,
		maxSize: 220,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:100,	 
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						disabled:true,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						readOnly: true,
						disabled:true,
						name: 'btsname'
					}, {
						xtype:'textfield',
						fieldLabel: '油机编号',
						width:150,
						name: 'danymoSeiral'
					}, 
					dynamoIsStaticCombo
					,{
						xtype:'textfield',
						fieldLabel: '功率',
						width:150,
						disabled:true,
						name: 'power'
					},
					modelCombo,
					houseCombo
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '型号',
						width:150,
						disabled:true,
						name: 'type'
					},modelCombo, 
					{
						xtype:'textfield',
						fieldLabel: '厂家',
						width:150,
						disabled:true,
						name: 'manufacturer'
					}
					, {
						xtype:'textfield',
						fieldLabel: '耗油量(升/小时)',
						width:150, 
						value:-1,
						disabled:true,
						name: 'consumption'
					}
					, {
						xtype:'textfield',
						fieldLabel: '更换及维修记录',
						width:150,
						disabled:true,
						name: 'maintenance'
					}]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '累计发电时长',
						width:150,
						value:-1,
						disabled:true,
						name: 'sumtime'
					}
					,{
						xtype:'textfield',
						fieldLabel: '徒步运送油机距离',
						width:150,
						disabled:true,
						name: 'dynamoWorkLen'
					}, 
					autobootCombo
					,{
						xtype:'textfield',
						fieldLabel: '保养记录',
						width:150,
						disabled:true,
						name: 'dynamoMaintainRec'
					}					
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						disabled:true,
						disabled:true,
						name: 'memo'
					}]
			 }
			] 	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'DynamoTab',
		items    : [
			dynamoGrid,
			dynamoFormPanel
		]
	});
	
	
	function savedynamo()
	{
		if (!dynamoFormPanel.getForm().isValid())
			return;	
			
		var form = dynamoFormPanel.getForm().getValues(false);	
		DynamoBO = {};
		DynamoBO.id = form.id;
		DynamoBO.btscode = form.btscode;
		DynamoBO.btsname= form.btsname;
		DynamoBO.manufacturer = form.manufacturer;
		DynamoBO.type = form.type;		
		DynamoBO.model = form.model;
		DynamoBO.power = form.power;
		DynamoBO.autoboot = form.autoboot;		
		DynamoBO.consumption = form.consumption;
		DynamoBO.sumtime = form.sumtime; 		
		DynamoBO.maintenance = form.maintenance;	
		DynamoBO.memo = form.memo;	
		DynamoBO.dynamoIsStatic = form.dynamoIsStatic;		
		DynamoBO.danymoSeiral = form.danymoSeiral;
		DynamoBO.dynamoWorkLen = form.dynamoWorkLen;
		DynamoBO.dynamoMaintainRec = form.dynamoMaintainRec;	
		DynamoBO.dynamoHouse = form.dynamoHouse;	
		
	   			
		JDynamoWeb.saveDynamo(DynamoBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(dynamoDS,dynamoFormPanel); 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	
	loadDate(dynamoDS,dynamoFormPanel);
		
	
})
